from django import forms
from django.forms import Form, ModelForm, CharField, TimeField, DateField, FloatField, IntegerField
from django.forms.widgets import HiddenInput
from main.models import Excursion, Outcrop, Image
import datetime

YES_OR_NO = (
    (True, 'Public'),
    (False, 'Private')
)
valid_time_formats = ['%I:%M%p', '%I:%M %p']

"""
Excursion model form to allow an excursion to be created or updated.
""" 
class ExcursionModelForm(ModelForm):
    name = CharField(max_length=256)
    password = CharField(required=False, max_length=16, widget=forms.PasswordInput(render_value=True))
    description =  CharField(required=False, widget=forms.Textarea())
    public = forms.BooleanField(required=False, initial=False)
    date = DateField(initial=datetime.date.today(), widget=forms.DateInput(attrs={'id':'datepicker'}, format = '%d/%m/%Y'), input_formats=('%d/%m/%Y',), localize=True)
    time = TimeField(required=False, widget=forms.TimeInput(attrs={'id':'spinner'}, format='%I:%M %p'), input_formats=valid_time_formats)
    class Meta:
        model = Excursion
        fields = ('name', 'password', 'description', 'public','date','time')
        widgets = {
            'password': forms.PasswordInput(render_value=True), 'description': forms.Textarea(),
            'date':forms.DateInput(attrs={'id':'datepicker'}, format = '%d/%m/%Y'), 'time':forms.TimeInput(attrs={'id':'spinner'}, format='%I:%M %p')
        }
        
    def form_valid(self, form):
        form.instance.owner = self.request.user
        return super(ExcursionModelForm, self).form_valid(form)

class ImageForm(ModelForm):
    name = CharField(max_length=256)
    notes = CharField(required=False, widget=forms.Textarea())
    
    class Meta:
        model= Image
        fields = ('name', 'notes')

"""
Outcrop model form to allow an excursion to be created or updated.
""" 
class OutcropModelForm(ModelForm):
    name = CharField(max_length=254)
    description = CharField(required=False, widget=forms.Textarea())
    longitude = FloatField(widget=HiddenInput(attrs={'id':'longitude'}))
    latitude = FloatField(widget=HiddenInput(attrs={'id':'latitude'}))
    class Meta:
        model = Outcrop
        fields = ('name', 'description', 'longitude', 'latitude')
        
    def form_valid(self, form):
        form.instance.owner = self.request.user
        return super(ExcursionModelForm, self).form_valid(form)

class ExistingOutcropForm(Form):
    key = IntegerField(widget=HiddenInput(attrs={'id':'key'}));

class OutcropForm(Form):
    name = CharField(max_length=254)
    description = CharField(required=False, widget=forms.Textarea())
    longitude = FloatField(widget=HiddenInput(attrs={'id':'longitude'}))
    latitude = FloatField(widget=HiddenInput(attrs={'id':'latitude'}))
    
class JoinPrivateExcursion(Form):
    password = CharField(required=True, max_length=16, widget=forms.PasswordInput())
    
class SearchBar(Form):
    q = CharField(required=False)